/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.config;

import com.google.gwt.user.client.ui.*;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;

public class SyslogConfigDialogBox extends Window {

    final TextBox keywordTextBox = new TextBox();

    final TextBox ipTextBox = new TextBox();

    final TextBox filePathTextBox = new TextBox();

    final ListBox sensorsComboBox = new ListBox();

    boolean cancelled = false;

    boolean isNew = false;

    public SyslogConfigDialogBox(SensorConfigTable config) {
        super();
        final SyslogConfigDialogBox note = this;
        this.setShadow(true);
        this.setFrame(true);
        this.setModal(true);
        final SensorConfigTable tempConfig = config;
        FlexTable table = new FlexTable();

        int idx = -1;

        table.setHTML(++idx, 0, "<b>Sensor</b>");
        table.setWidget(++idx, 0, sensorsComboBox);
        table.setHTML(++idx, 0, "<b>File Path</b>");
        table.setWidget(++idx, 0, filePathTextBox);
        table.setHTML(++idx, 0, "<b>Tag</b>");
        table.setWidget(++idx, 0, keywordTextBox);
        table.setHTML(++idx, 0, "<b>IP</b>");
        table.setWidget(++idx, 0, ipTextBox);

        HorizontalPanel buttons = new HorizontalPanel();

        Button cancelButton = new Button();
        cancelButton.setText("Cancel");
        cancelButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                cancelled = true;
                note.hide();

            }
        });

        Button okButton = new Button();
        okButton.setText("OK");
        okButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {

                String filePath = filePathTextBox.getText().trim();
                if (filePath.endsWith(":")) {
                    filePath = filePath.substring(0, filePath.length() - 1);
                }

                if (filePath.length() == 0) {
                    MessageBox.alert("You must specify a file path.");
                    return;
                }

                if (keywordTextBox.getText().trim().length() == 0) {
                    MessageBox.alert("You must specify a keyword (tag).");
                    return;
                }

                if (ipTextBox.getText().trim().length() > 0) {
                    String[] ip = ipTextBox.getText().trim().split("\\.");
                    try {
                        int v1 = Short.parseShort(ip[0]);
                        int v2 = Short.parseShort(ip[1]);
                        int v3 = Short.parseShort(ip[2]);
                        int v4 = Short.parseShort(ip[3]);
                    } catch (Exception e) {
                        MessageBox.alert("You must specify a valid IP address or leave this field blank for 'none'.");
                        return;
                    }
                }

                SyslogConfig syslogConfig = getSyslogConfig();
                if (isNew()) {
                    tempConfig.addSyslogConfig(syslogConfig, sender);
                } else {
                    tempConfig.editSyslogConfig(syslogConfig, sender);
                }
                tempConfig.syslogConfigs();

                cancelled = false;
                note.hide();

            }
        });

        Button resetButton = new Button();
        resetButton.setText("Reset");
        resetButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                reset();
            }
        });

        buttons.add(resetButton);
        buttons.add(okButton);
        buttons.add(cancelButton);

        table.setWidget(++idx, 0, buttons);
        this.add(table);

    }

    public SyslogConfig getSyslogConfig() {
        SyslogConfig cse = new SyslogConfig();
        cse.filePath = filePathTextBox.getText().trim();
        cse.keyword = keywordTextBox.getText().trim();
        cse.ip = ipTextBox.getText().trim();
        cse.sensor = Integer.parseInt(sensorsComboBox.getValue(sensorsComboBox.getSelectedIndex()));
        return cse;
    }

    public void set(SyslogConfig sc) {
        keywordTextBox.setText(sc.keyword);
        filePathTextBox.setText(sc.filePath);
        ipTextBox.setText(sc.ip);
        for (int i = 0; i < sensorsComboBox.getItemCount(); i++) {
            try {
                int sensor = Integer.parseInt(sensorsComboBox.getValue(i));
                if (sensor == sc.sensor) {
                    sensorsComboBox.setSelectedIndex(i);
                    break;
                }
            } catch (NumberFormatException nme) {
                // no op
            }
        }
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public void setSensors(SensorInfo[] sensors) {
        sensorsComboBox.setEnabled(true);
        sensorsComboBox.clear();
        for (int i = 0; i < sensors.length; i++) {
            if (sensors[i].isSyslog) {
                sensorsComboBox.addItem(sensors[i].sensorName, sensors[i].id + "");
            }
        }
        if (sensorsComboBox.getItemCount() == 0) {
            sensorsComboBox.addItem("No syslog sensors configured");
            sensorsComboBox.setEnabled(false);
        }
    }

    public boolean isNew() {
        return isNew;
    }

    public void setNew(boolean isNew) {
        this.isNew = isNew;
        if (isNew) {
            sensorsComboBox.setEnabled(true);
            reset();
        } else {
            sensorsComboBox.setEnabled(false);
        }
    }

    private void reset() {
        filePathTextBox.setText("");
        keywordTextBox.setText("");
        ipTextBox.setText("");
        sensorsComboBox.setSelectedIndex(0);
    }
}
